<template>
  <div>
    <van-cell>
      <template>
        <div class="user-box">
          <van-image fit="cover" round :src="currentCmt.aut_photo" />
          <div class="title">
            <div class="autor">{{ currentCmt.aut_name }}</div>
            <div>{{ currentCmt.content }}</div>
            <div class="answer">
              <span class="time">{{ currentCmt.pubdate | relaTime }}</span>
            </div>
          </div>
        </div>
      </template>
    </van-cell>

    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>

    <!-- list显示回复列表 -->
    <van-list
      class="cmt-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="(item, index) in list" :key="item.id">
        <template>
          <div class="user-box">
            <van-image fit="cover" round :src="item.aut_photo" />
            <div class="title">
              <div class="autor">{{ item.aut_name }}</div>
              <div>{{ item.content }}</div>
              <div class="answer">
                <span class="time">{{ item.pubdate | relaTime }}</span>
              </div>
            </div>
            <div @click="lightHeart(index)">
              <van-icon
                name="like"
                size="14"
                color="red"
                v-if="item.is_liking"
              />
              <van-icon name="like-o" size="14" v-else />
            </div>
          </div>
        </template>
      </van-cell>
    </van-list>
    <!-- 发表回复的区域 -->
    <div class="send-box">
      <van-field
        @keyup.enter="send"
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { cmtListAPI, addCmtListAPI } from '@/api/comment'
export default {
  props: {
    currentCmt: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      // 跟发表的输入框双向绑定
      cmt: '',
      offset: null
    }
  },
  methods: {
    lightHeart(index) {
      this.list[index].is_liking = !this.list[index].is_liking
    },
    async onLoad() {
      const res = await cmtListAPI({
        type: 'c',
        source: this.currentCmt.com_id,
        offset: this.offset
      })

      // 把本次返回的last_id作为offset方便下次传递
      this.offset = res.data.last_id
      this.list.push(...res.data.results)

      // 加载完数据把loading改为false
      this.loading = false

      // 如果结束id等于本次的最后一条id，就代表本次请求后加载完了
      if (res.data.end_id == res.data.last_id) {
        this.finished = true
      }
    },
    async send() {
      if (this.cmt == '') return this.$toast('内容不能为空')
      const res1 = await addCmtListAPI({
        target: this.currentCmt.com_id,
        content: this.cmt,
        art_id: this.$route.query.id
      })
      this.list.unshift(res1.data.new_obj)

      this.$toast.success('发表成功')
      // 回复发表数量加1
      this.currentCmt.reply_count++

      this.cmt = ''
    }
  }
}
</script>

<style lang="less" scoped>
.user-box {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  .title {
    flex: 1;
    .autor {
      font-weight: 600;
    }
    .answer {
      display: flex;
      align-items: center;
      .time {
        color: gray;
        font-size: 12px;
        margin-right: 10px;
      }
    }
  }
  .van-image {
    width: 60px;
    height: 60px;
    margin-right: 15px;
  }
}
</style>
